<?php

namespace App\Listeners\OpenShop;

use App\Enums\OpenShop\InvestmentPromotion\SiteMissionEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Events\OpenShop\SignSiteMissionEvent;
use App\Listeners\BaseListener;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSiteMissionModel;
use App\Service\OpenShop\Feishu\MissionTemplateService;
use App\Service\OpenShop\Feishu\SiteMissionService;
use App\Service\OpenShop\OrderService;
use Illuminate\Support\Facades\Log;

class SignSiteMissionListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Handle the event.
     *
     * @param SignSiteMissionEvent $event
     * @return void
     */
    public function handle(SignSiteMissionEvent $event)
    {
        $logData = [
            'orderNo'    => $event->orderNo,
            'stepStatus' => $event->nowStepStatus,
            'dealUid'    => $event->dealUid,
            'finishNowStep' => $event->finishNowStep,
        ];
        // 写入
        Log::channel('siteMission')->info('任务事件-流程1-监听开始写入:成功', $logData);

        $openShopSiteMissionModel = new OpenShopSiteMissionModel();

//        $missionInfo = $openShopSiteMissionModel->getInfo($event->orderNo, $event->nowStepStatus);

//        if (empty($missionInfo)) {
//            Log::channel('siteMission')->error('任务事件-流程1-处理失败:无此任务', $logData);
//            return;
//        }

        $nowStepStatus = OpenShopMainProcessModel::query()->where('orderNo', $event->orderNo)->value('nowStepStatus');
        if ($nowStepStatus != $event->nowStepStatus) {
            Log::channel('siteMission')->error("任务事件-流程1-处理失败:当前任务状态不匹配 {$nowStepStatus} {$event->nowStepStatus}");
            return;
        }

        if ($event->finishNowStep) {

            // 更新任务处理状态
            $openShopSiteMissionModel->updateData($event->orderNo, $event->nowStepStatus, [
                'status'       => SiteMissionEnum::STATUS_2,
                'updatedByUid' => $event->dealUid,
                'dealUid'      => $event->dealUid,
                'completeAt'   => time()
            ]);
        }

        Log::channel('siteMission')->info('任务事件-流程2-更新任务处理:成功', $logData);

        // 获取下一步任务信息
        $nextStepStatus = (new MissionTemplateService())->getNextStepStatus($event->orderNo, $event->nowStepStatus);
        if ($nextStepStatus == -1) {

            // 状态完结
            OpenShopMainProcessModel::updateNowStepStatus($event->orderNo, MissionTemplateEnum::STEP_STATUS_999);
            Log::channel('siteMission')->error('任务事件-流程2-处理失败:无下一步任务', $logData);
            return;
        }

        // 更新主表的当前nowStepStatus
        OpenShopMainProcessModel::updateNowStepStatus($event->orderNo, $nextStepStatus);

        // 判断是否已经添加下一步任务
        $nextMissionInfo = $openShopSiteMissionModel->getInfo($event->orderNo, $nextStepStatus);
        if ($nextMissionInfo) {
            Log::channel('siteMission')->info('任务事件-流程2-新增任务处理:失败,重复处理', $logData);
            return;
        }

        // 跳过新建任务
        $skipStep = [];

        if (!in_array($nextStepStatus, $skipStep)) {
            // 新增任务
            SiteMissionService::genMission($event->orderNo, $nextStepStatus);
            Log::channel('siteMission')->info('任务事件-流程3-新增任务处理:成功', $logData);
        }

        // 任务后的事件
        OrderService::updateOrderPermissionAnchor($event->orderNo);
    }
}
